﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using FancyBattles.GameEntities;

namespace FancyBattles.GameScreen.StateMachine
{
    class SelectState : State
    {

        public SelectState(GameplayScreen parentScreen, PlayerIndex player)
            : base(parentScreen, player)
        {
            foreach (BouncingBitch bitch in BouncingBitch.BitchesList)
            {
                bitch.ChangeTexture(BouncingBitch.animationState.Idle);
            }
        }

        public override void Update(GameTime gameTime)
        {
            if (InputHandler.IsFire(player))
            {
                ParentScreen.ScreenStates[player] = new WaitingState(ParentScreen, player);
            }
            if (InputHandler.isConfirm(player))
            {
                if ((from bitch in BouncingBitch.BitchesList where bitch.IsSelected == true && bitch.Player.Equals(player) select bitch).First().GetType().Equals(typeof(Archer)))
                {
                    ParentScreen.ScreenStates[player] = new DefineShotState(ParentScreen, player);
                }
                else
                {
                    ParentScreen.ScreenStates[player] = new DefineMoveState(ParentScreen, player);
                }
            }
            if (InputHandler.isMenuDown(player))
            {
                LinkedList<BouncingBitch> selectedList;
                if (player.Equals(PlayerIndex.One))
                {
                     selectedList = BouncingBitch.FirstBitchesList;
                }
                else
                {
                    selectedList = BouncingBitch.SecondBitchesList;
                }
                BouncingBitch selectedBitch = (from bitch in selectedList where bitch.IsSelected == true select bitch).First();
                selectedBitch.IsSelected = false;
                selectedBitch.ChangeTexture(BouncingBitch.animationState.Idle);
                try
                {
                    selectedList.Find(selectedBitch).Next.Value.IsSelected = true;
                    selectedList.Find(selectedBitch).Next.Value.ChangeTexture(BouncingBitch.animationState.Selected);
                }
                catch
                {
                    selectedList.First.Value.IsSelected = true;
                    selectedList.First.Value.ChangeTexture(BouncingBitch.animationState.Selected);
                }
            }
            if (InputHandler.isMenuUp(player))
            {
                LinkedList<BouncingBitch> selectedList;
                if (player.Equals(PlayerIndex.One))
                {
                    selectedList = BouncingBitch.FirstBitchesList;
                }
                else
                {
                    selectedList = BouncingBitch.SecondBitchesList;
                }
                BouncingBitch selectedBitch = (from bitch in selectedList where bitch.IsSelected == true select bitch).First();
                selectedBitch.IsSelected = false;
                selectedBitch.ChangeTexture(BouncingBitch.animationState.Idle);
                try
                {
                    selectedList.Find(selectedBitch).Previous.Value.IsSelected = true;
                    selectedList.Find(selectedBitch).Previous.Value.ChangeTexture(BouncingBitch.animationState.Selected);
                }
                catch
                {
                    selectedList.Last.Value.IsSelected = true;
                    selectedList.Last.Value.ChangeTexture(BouncingBitch.animationState.Selected);
                }
            }

        }

        public override void Draw(GameTime gameTime)
        {
        }

    }
}
